package com.central.apps.handler.contract;

import com.central.app.entity.TBFile;
import com.central.app.service.CheckConNodeService;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2022/9/21.
 */
@Slf4j
@Component
@Service
public class NodeCheckHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private CheckConNodeService checkConNodeService;
    @Autowired
    private ConchangeHandler conchangeHandler;
    @Autowired
    private FileSynUtilService fileSynUtilService;

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {

        System.out.println("流程审批后，进入代码==============================flowNode.getId() =========" + flowNode.getId());
        //提交申请时，合同状态就应该是执行中
        if("submit".equals(flowNode.getId())){
            try {
                String sql3 = "select * from gq_nodecheck where id = " + id;
                List<Map<String, Object>> list3 = handlerService.findBySql(sql3);
                //把上传的文件添加到合同节点位置
                String nodecode = (String) list3.get(0).get("nodecode");//节点编号
                String concode = (String) list3.get(0).get("concode");//合同编号
                conchangeHandler.updateStatus(concode,"执行中");
                String sql4 = "update gq_nodecheck set nodestatus = '执行中' where id = " + id;
                handlerService.updateBySql(sql4);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (flowNode.getId().equals("approvaluser")) {
            System.out.println("开始执行update");
            boolean flag = op.isFlag();//审批通过之后，把节点状态变为已完成

            String sql3 = "select * from gq_nodecheck where id = " + id;
            List<Map<String, Object>> list3 = handlerService.findBySql(sql3);
            //把上传的文件添加到合同节点位置
            String nodecode = (String) list3.get(0).get("nodecode");//节点编号
            String concode = (String) list3.get(0).get("concode");//合同编号
            String ispaynode= (String)list3.get(0).get("ispaynode");//是否收付款
            String iscollcon= (String)list3.get(0).get("iscollcon");//合同类别
            //合同状态修改成执行中
//            String accountSql = "update gq_conaccount set constatus ='执行中' where concode = '" + concode + "'";
//            handlerService.updateBySql(accountSql);
//            String sqlinfo = "update gq_coninfo set constatus ='执行中'  where concode = '" + concode + "'";
//            handlerService.updateBySql(sqlinfo);

            //查询节点表 主键id
            String sql2 = "select * from gq_connodenew where nodecode ='" + nodecode + "' and concode = '" + concode + "'";
            List<Map<String, Object>> list = handlerService.findBySql(sql2);
            Long nodeid = 0L;//合同节点表的主键id
            if (CollectionUtils.isNotEmpty(list)) {
                nodeid = MapUtils.getLong( list.get(0),"id");

                List<TBFile> tbFiles = checkConNodeService.selectFiles("pro"+app, id );
                System.out.println("===================tbFiles============" + tbFiles);
                if (CollectionUtils.isNotEmpty(tbFiles)) {
                    for (TBFile tbFile : tbFiles) {
                        tbFile.setFileid(null);
                        tbFile.setAppname("proconinfoApp");
                        tbFile.setRecordid(nodeid);
                        System.out.println("================tbFile.toString()========" + tbFile.toString());
                        //插入到合同节点数据中
                        checkConNodeService.insertConfiles(tbFile);
                    }

                }
                List<TBFile> tbFiles2 = checkConNodeService.selectFiles("fin"+app, id );
                System.out.println("===================tbFiles============" + tbFiles2);
                if (CollectionUtils.isNotEmpty(tbFiles2)) {
                    for (TBFile tbFile : tbFiles2) {
                        tbFile.setFileid(null);
                        tbFile.setAppname("finconinfoApp");
                        tbFile.setRecordid(nodeid);
                        System.out.println("================tbFile.toString()========" + tbFile.toString());
                        //插入到合同节点数据中
                        checkConNodeService.insertConfiles(tbFile);
                    }

                }
                //插入到合同台账
                fileSynUtilService.insertFiles(app, id, concode);
                //查询合同下的所有节点是否已完成
                String sqlcon = "select * from gq_connodenew where (status='未开始' or status = '执行中') and  concode = '" + concode + "'";
                List<Map<String, Object>> listcon = handlerService.findBySql(sqlcon);
                if (!CollectionUtils.isNotEmpty(listcon)) {
//                    String accountSql2 = "update gq_conaccount set constatus ='待验收' where constatus = '执行中' and  concode = '" + concode + "'";
//                    handlerService.updateBySql(accountSql2);
//                    String sql5 = "update gq_coninfo set constatus ='待验收'   where constatus = '执行中' and  concode = '" + concode + "'";
//                    handlerService.updateBySql(sql5);
                    try {
                        conchangeHandler.updateStatus(concode,"待验收");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                //流程归档
                fileSynUtilService.processarc(app, id + "", "节点完成检查", (String) list.get(0).get("concode"), (String) list.get(0).get("conname"));
                //更新节点状态
                String nodeStatus = "进行中";
                if (flag&&"否".equals(ispaynode)) {
                    nodeStatus = "已完成";
                }
                if (flag&&"是".equals(ispaynode)&&"收款合同".equals(iscollcon)) {
                    nodeStatus = "待收款";
                }
                if (flag&&"是".equals(ispaynode)&&"付款合同".equals(iscollcon)) {
                    nodeStatus = "待付款";
                }
                String sql4 = "update gq_nodecheck set nodestatus = '" + nodeStatus + "' where id = " + id;
                handlerService.updateBySql(sql4);
                //更新节点状态
//                String sql5 = "update gq_connodenew set status = '" + nodeStatus + "' where id = " + nodeid;
//                handlerService.updateBySql(sql5);
                Map<String,Object> map = new HashMap<>();
                map.put("id",nodeid);
                map.put("status",nodeStatus);
                map.put("terevopinion",list3.get(0).get("terevopinion"));
                map.put("comrevopinion",list3.get(0).get("comrevopinion"));
                map.put("lerevopinion",list3.get(0).get("lerevopinion"));
                handlerService.update("gq_connodenew",map);
                //更新合同节点进度
                try {
                    fileSynUtilService.updateprocess(concode);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }


    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
//        if ("gq_nodecheck".equals(objName)) {
//            String nodecode = (String) data.get("nodecode");
//            //查询节点表 主键id
//            String sql2 = "select * from gq_connode where nodecode ='" + nodecode + "'";
//            List<Map<String, Object>> list = handlerService.findBySql(sql2);
//            int nodeid = 0;
//            int id = (Integer) data.get("id");
//            if (!CollectionUtils.isEmpty(list)) {
//                nodeid = (Integer) list.get(0).get("id");
//            }
//            String sql = "select * from  tb_file where appname = '" + app + "' and recordid =" + id;
//            List<Map<String, Object>> fileLists = handlerService.findBySql(sql);
//            if (!CollectionUtils.isEmpty(fileLists)) {
//                String del = "delete from tb_file where appname = 'connodeApp' and recordid = '"+nodeid+"'";
//                handlerService.updateBySql(del);
//                for (Map<String, Object> fileList : fileLists) {
//                    fileList.put("fileid", null);
//                    fileList.put("appname", "connodeApp");
//                    fileList.put("recordid", nodeid);
//                    handlerService.insert("tb_file", fileList);
//                }
//            }
//
//
//        }
    }
}
